package tpf.designer.pattern.course9.homework.exercise3;

import org.pentaho.reporting.engine.classic.core.DataFactory;
import org.pentaho.reporting.engine.classic.core.MasterReport;
import org.pentaho.reporting.engine.classic.core.TableDataFactory;
import org.pentaho.reporting.engine.classic.core.wizard.RelationalAutoGeneratorPreProcessor;
import tpf.designer.pattern.course9.AbstractReportGenerator;

import javax.swing.table.AbstractTableModel;
import java.util.Map;

public class Sample4 extends AbstractReportGenerator {

    private static final String QUERY_NAME = "ReportQuery";

    private static class Sample4TableModel extends AbstractTableModel {

        /**
         * The sample data
         */
        private static final Object[][] data = new Object[][]
                {
                        new Object[]{"Acme Industries", 2500, 18.75},
                        new Object[]{"Brookstone Warehouses", 5000, 36.1245},
                        new Object[]{"Cartwell Restaurants", 18460, 12.9},
                        new Object[]{"Domino Builders", 20625, 45.52},
                        new Object[]{"Elephant Zoo Enclosures", 750, 19.222},
                };

        /**
         * 添加表头信息
         * @param column
         * @return
         */
        public String getColumnName(int column) {
            String result = "";
            for (; column >= 0; column = column / 26 - 1) {
                result = (char)((char)(column%26)+'1') + result;
            }
            return result;
        }

        @Override
        public int getRowCount() {
            return data.length;
        }

        @Override
        public int getColumnCount() {
            return data[0].length;
        }

        @Override
        public Object getValueAt(int rowIndex, int columnIndex) {
            if (rowIndex >= 0 && rowIndex < data.length && columnIndex >= 0 && columnIndex < data[rowIndex].length) {
//                System.out.println(data[rowIndex][columnIndex]);
                return data[rowIndex][columnIndex];
            }

            return null;
        }
    }

    @Override
    public MasterReport getReportDefinition() {
        // Create a report using the autogenerated fields
        final MasterReport report = new MasterReport();
        report.addPreProcessor(new RelationalAutoGeneratorPreProcessor());

        // Add the data factory to the report
        report.setDataFactory(new TableDataFactory(QUERY_NAME, new Sample4TableModel()));
        report.setQuery(QUERY_NAME);

        // return
        return report;
    }

    @Override
    public DataFactory getDataFactory() {
        /*final DriverConnectionProvider sampleDriverConnectionProvider = new DriverConnectionProvider();
        sampleDriverConnectionProvider.setDriver("net.sourceforge.jtds.jdbc.Driver");
        sampleDriverConnectionProvider.setUrl("jdbc:jtds:sybase://10.200.168.75:7000/symarket60;Charset=gbk");
        sampleDriverConnectionProvider.setProperty("user", "dbo50");
        sampleDriverConnectionProvider.setProperty("password", "password");

        final SQLReportDataFactory dataFactory = new SQLReportDataFactory(sampleDriverConnectionProvider);
        dataFactory.setQuery(QUERY_NAME,
                "SELECT * FROM Tbb_Dept");

        return dataFactory;*/

        return null;
    }

    @Override
    public Map<String, Object> getReportParameters() {
        return null;
    }
}
